<?php
try {
	/*
	 * 处理伊顿考勤同步信息，用于从伊顿数据库中 同步考勤数据到本地数据库，进行计算使用
     */
	@header ( 'Content-type:text/html;charset=UTF-8' );

	define ( 'MacTop', true );
	define ( 'Root', dirname ( __FILE__ ) );
	require_once Root . '/common.inc.php';
	require_once Root . '/function.inc.php';

	//获取当前数据库最大id
	GetMyConnection ();
	$sql='select max(ID) from StudentAttendance';
	$result = mysql_query ( $sql, $db );
	$maxid=mysql_result($result,0);
	echo $maxid."\n";
	
    putenv('ODBCSYSINI=/etc'); 
    putenv('ODBCINI=/etc/odbc.ini'); 
    $msdb = new PDO('odbc:EtonkidsBridge','EtonkidsBridge','EtonkidsBridge!$'); 
	//$msdb = new PDO("odbc:Driver={EtonkidsBridge};Server=tcp:cip.etonkids.com,1433;Database=EtonkidsBridge;",'EtonkidsBridge','EtonkidsBridge!$'); 		
    //让所有数据库错误抛出异常
   // $msdb->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
	
   $sql='SELECT top 1000 * FROM EtonkidsBridge.dbo.StudentAttendance where ID>'.$maxid .' order by ID asc' ;
   echo $sql."\n";
   $result = $msdb->query($sql)->fetchAll(PDO::FETCH_ASSOC);
   $msdb = null;
 	echo count($result)."\n";

   if($result){
		$str='';
		foreach($result as $key => $value){
			$str .='(';
			foreach($value as $key1 => $value1){
				$str .="'$value1',";
			}
			$str=substr($str,0,-1);
			$str .='),';
			$maxid=$value['ID'];
		}
	   $sql='insert StudentAttendance (`ID`,`ExactId`,`StudentNo`,`CheckTime`,`LastCheckTime`,`CreateDate`,`AttendanceStatus`) values '.substr($str,0,-1);
	  // echo $sql;
	   $insertresult = mysql_query ( $sql, $db );
		//echo $sql."\n";
		  //echo "insert over\n";
   }
   
   //print_r($result);
   echo $maxid ." is over\n";
	CleanUpDB ();
	exit ();
} catch ( Exception $e ) {
	CleanUpDB ();
	$msdb = null;
	echo $e->getMessage ();
	exit ();
}
?>